package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sky.constant.StatusConstant;
import com.sky.entity.Dish;
import com.sky.entity.Order;
import com.sky.entity.Setmeal;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetMealMapper setMealMapper;

    /**
     * 查询营业数据
     *
     * @return
     */
    @Override
    public BusinessDataVO getBusinessData() {
        LocalDate now = LocalDate.now();
        Map<String, Object> map = ReportServiceImpl.getBeginAndEndTime(now);
        Integer orderCount = orderMapper.countOrder(map).intValue();
        map.put("status", Order.COMPLETED);
        Double turnOver = orderMapper.sumAccountByMap(map).doubleValue();
        Integer validOrderCount = orderMapper.countOrder(map).intValue();
        Double orderCompletionRate = validOrderCount.doubleValue() / orderCount.doubleValue();
        Double unitPrice = turnOver / validOrderCount;
        // 保留两位小数，四舍五入
        BigDecimal bd = new BigDecimal(unitPrice.toString());
        bd = bd.setScale(2, RoundingMode.HALF_UP); // 关键：设置保留位数和舍入模式
        unitPrice = bd.doubleValue(); // 转回 Double 类型
        Integer newUsers = orderMapper.sumNewUserByTime((LocalDateTime) map.get("begin"), (LocalDateTime) map.get("end")).intValue();
        return new BusinessDataVO(
                turnOver,
                validOrderCount,
                orderCompletionRate,
                unitPrice,
                newUsers
        );
    }

    /**
     * 查询订单统计数据
     *
     * @return
     */
    @Override
    public OrderOverViewVO getOverviewOrders() {
        LocalDate now = LocalDate.now();
        Map<String, Object> map = ReportServiceImpl.getBeginAndEndTime(now);
        log.info("查询订单统计数据");
        map.put("status", Order.TO_BE_CONFIRMED);
        Integer waitingOrders = orderMapper.countOrder(map).intValue();
        map.put("status", Order.DELIVERY_IN_PROGRESS);
        Integer deliveredOrders = orderMapper.countOrder(map).intValue();
        map.put("status", Order.COMPLETED);
        Integer completedOrders = orderMapper.countOrder(map).intValue();
        map.put("status", Order.CANCELLED);
        Integer cancelledOrders = orderMapper.countOrder(map).intValue();
        map.put("status", null);
        Integer allOrders = orderMapper.countOrder(map).intValue();
        return new OrderOverViewVO(
                waitingOrders,
                deliveredOrders,
                completedOrders,
                cancelledOrders,
                allOrders
        );
    }

    /**
     * 菜品总览
     *
     * @return
     */
    @Override
    public DishOverViewVO getOverviewDishes() {
        LambdaQueryWrapper<Dish> queryWrapper1 = new LambdaQueryWrapper<Dish>()
                .eq(Dish::getStatus, StatusConstant.ENABLE);
        List<Dish> dishes1 = dishMapper.selectList(queryWrapper1);

        LambdaQueryWrapper<Dish> queryWrapper2 = new LambdaQueryWrapper<Dish>()
                .eq(Dish::getStatus, StatusConstant.DISABLE);
        List<Dish> dishes2 = dishMapper.selectList(queryWrapper2);

        return new DishOverViewVO(dishes1.size(), dishes2.size());
    }


    @Override
    public SetmealOverViewVO getOverviewSetmeals() {
        LambdaQueryWrapper<Setmeal> queryWrapper1 = new LambdaQueryWrapper<Setmeal>()
                .eq(Setmeal::getStatus, StatusConstant.ENABLE);
        List<Setmeal> setmeals1 = setMealMapper.selectList(queryWrapper1);
        LambdaQueryWrapper<Setmeal> queryWrapper2 = new LambdaQueryWrapper<Setmeal>()
                .eq(Setmeal::getStatus, StatusConstant.DISABLE);
        List<Setmeal> setmeals2 = setMealMapper.selectList(queryWrapper2);
        return new SetmealOverViewVO(
                setmeals1.size(),
                setmeals2.size()
        );
    }
}
